package com.yy.wga.wechat.service.manager;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
import com.yy.wga.common.core.enmus.EResponseCode;
import com.yy.wga.common.core.exception.BusinessException;
import com.yy.wga.wechat.feign.request.WechatQrCodeGetReq;
import com.yy.wga.wechat.service.config.WxMaConfiguration;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.stereotype.Component;

/**
 * @Author: gc
 * @Date: 2024/12/10
 * @Description:
 **/
@Slf4j
@Component
public class WechatQrCodeManager {
    public String getQrCode(WechatQrCodeGetReq request) {
        WxMaService maService = WxMaConfiguration.getMaService();
        WxMaQrcodeService qrcodeService = maService.getQrcodeService();

        // 设置小程序二维码线条颜色为黑色
        WxMaCodeLineColor lineColor = new WxMaCodeLineColor("0", "0", "0");
        try {
            byte[] wxaCodeUnlimitBytes = qrcodeService.createWxaCodeUnlimitBytes(request.getScene(),
                    request.getPath(), false, request.getEnvVersion(), 430, false, lineColor, false);
            return Base64.encodeBase64String(wxaCodeUnlimitBytes);
        } catch (WxErrorException e) {
            log.error("生成小程序码异常:{}", e.getMessage(), e);
            throw new BusinessException(EResponseCode.SYSTEM_BUSY_ERROR);
        }
    }
}
